package com.example.xmlfind.entity;

import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

/**
 * 单个搜索结果
 */
public class FindResult implements Comparable<FindResult> {
    PsiFile file;
    String keyValue;
    String lineText;
    int startOffsetInParent;

    public FindResult() {
    }

    public FindResult(PsiFile file, String keyValue, String lineText, int startOffsetInParent) {
        this.file = file;
        this.keyValue = keyValue;
        this.lineText = lineText;
        this.startOffsetInParent = startOffsetInParent;
    }

    public PsiFile getFile() {
        return file;
    }

    public String getKeyValue() {
        return keyValue;
    }

    public String getLineText() {
        return lineText;
    }

    public int getStartOffsetInParent() {
        return startOffsetInParent;
    }

    public void setFile(PsiFile file) {
        this.file = file;
    }

    public void setKeyValue(String keyValue) {
        this.keyValue = keyValue;
    }

    public void setLineText(String lineText) {
        this.lineText = lineText;
    }

    public void setStartOffsetInParent(int startOffsetInParent) {
        this.startOffsetInParent = startOffsetInParent;
    }

    // 属性值相等视为同一对象
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        FindResult that = (FindResult) o;
        return Objects.equals(keyValue, that.keyValue);
    }

    @Override
    public int hashCode() {
        return Objects.hash(keyValue);
    }

    @Override
    public String toString() {
        return lineText;
    }

    @Override
    public int compareTo(@NotNull FindResult o) {
        if (keyValue == null && o.keyValue == null) return 0;
        if (keyValue == null) return -1;
        if (o.keyValue == null) return 1;
        return keyValue.compareTo(o.keyValue);
    }
}
